﻿using FireStarter.Base;
using Firestarter.DB.Schema.BO;
using FireStarterModeller.UI;
using FireStarterModeller.UI.DatabaseConnection;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.BO.Loaders;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI.DatabaseConnection
{
    [TestFixture]
    public class TestDatabaseConnectionGridAndEditorControl
    {
        [TestFixtureSetUp]
        public void SetUpFixture()
        {
            GlobalUIRegistry.ControlFactory = new ControlFactoryWin();
            ClassDef.ClassDefs.Clear();
            ClassDef.ClassDefs.Add(new XmlClassDefsLoader(BOBroker.GetClassDefsXml(), new DtdLoader()).LoadClassDefs());
        }

        [SetUp]
        public void Setup()
        {
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        private DatabaseConnectionGridAndEditorControl CreateDatabaseConnectionGridAndEditorControl()
        {
            return new DatabaseConnectionGridAndEditorControl();
        }

        [Test]
        [Ignore("For Visual Testing")]
        public void Test__Visually()
        {
            //---------------Set up test pack-------------------
            var control = CreateDatabaseConnectionGridAndEditorControl();
            var databaseConnectionInfo = TestUtilsDatabaseConnectionInfo.CreateUnsavedValidDatabaseConnectionInfo();
            control.DatabaseConnectionInfos = new BusinessObjectCollection<DatabaseConnectionInfo>{ databaseConnectionInfo };
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            TestUtilsUI.ShowInVisualTestingForm(GlobalUIRegistry.ControlFactory, control, DockStyle.Fill);
            //---------------Test Result -----------------------
        }

        [Test]
        public void Test_Construct_EditorControl_ShouldBeDatabaseConnectionEditorControl()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var connectionInfoControl = new DatabaseConnectionGridAndEditorControl();
            var boEditorControl = connectionInfoControl.IBOEditorControl;
            //---------------Test Result -----------------------
            Assert.IsInstanceOf<DatabaseConnectionInfoEditorControl>(boEditorControl);
            Assert.AreSame(connectionInfoControl.BOGridAndEditorControl.IBOEditorControl, boEditorControl);
        }

        [Test]
        public void Test_TestConnectionButton_ShouldExistOnControl()
        {
            //---------------Set up test pack-------------------
            var connectionInfoControl = CreateDatabaseConnectionGridAndEditorControl();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var testConnectionButton = connectionInfoControl.TestConnectionButton;
            //---------------Test Result -----------------------
            var allChildControls = TestUtilsUI.GetAllChildControls(connectionInfoControl);
            Assert.IsTrue(allChildControls.Contains(testConnectionButton));
        }

        [Test]
        [Ignore("Unable to test MessageBox when TestButton clicked but works visually")]
        public void Test_TestConnectionButtonClick_ShouldCheckConnectionDetailsSetupCorrectly()
        {
            //---------------Set up test pack-------------------
            DatabaseConnectionInfo databaseConnectionInfo = TestUtilsDatabaseConnectionInfo.CreateSavedDatabaseConnectionInfo();
            var connectionInfoControl = CreateDatabaseConnectionGridAndEditorControl();
            IBOGridAndEditorControl boGridAndEditorControl = GetGridAndBOEditorControl(connectionInfoControl);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(boGridAndEditorControl.IBOEditorControl.BusinessObject);
            Assert.AreSame(databaseConnectionInfo, boGridAndEditorControl.IBOEditorControl.BusinessObject);
            //---------------Execute Test ----------------------
            connectionInfoControl.TestConnectionButton.PerformClick();
            //---------------Test Result -----------------------
        }

        private static IBOGridAndEditorControl GetGridAndBOEditorControl(DatabaseConnectionGridAndEditorControl connectionInfoControl)
        {
            return (IBOGridAndEditorControl)connectionInfoControl.Controls[0];
        }
    }

}
